package com.esm.sm800.sm800floating;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;

import java.util.Locale;

public class LocaleHelper {
    public static void setLocale(Context context, String languageCode, String countryCode) {
        Locale locale = new Locale(languageCode, countryCode);
        Locale.setDefault(locale);

        Configuration config = context.getResources().getConfiguration();
        config.locale = locale;
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

        // 保存语言设置到SharedPreferences
        SharedPreferences prefs = context.getSharedPreferences(GlobalUtil.PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(GlobalUtil.KEY_LANGUAGE, languageCode);
        editor.putString(GlobalUtil.KEY_COUNTRY, countryCode);
        editor.apply();
    }

    public static void restoreLocale(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(GlobalUtil.PREFS_NAME, Context.MODE_PRIVATE);
        String languageCode = prefs.getString(GlobalUtil.KEY_LANGUAGE, Locale.getDefault().getLanguage());
        String countryCode = prefs.getString(GlobalUtil.KEY_COUNTRY, Locale.getDefault().getCountry());
        setLocale(context, languageCode, countryCode);
    }
}
